import {Component, OnInit} from '@angular/core';
import {Banner} from '../../module/banner';
import {HttpService} from '../../service/http.service';
import {ProjectRank} from '../../module/projectRank';
import {HotSearch} from '../../module/hotSearch';
import {HeatProject} from '../../module/HeatProject';
import {BusinessRank} from '../../module/businessRank';
import {Money} from '../../module/money';
import {Title} from "@angular/platform-browser";

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  heatProjectEndId = 0;
  sliderList: Banner[];
  businessRankList: BusinessRank[];
  projectRankList: ProjectRank[];
  hotSearchList: HotSearch[];
  moneyPageList: Money[];
  moneyPageLists: Money[];
  flag = 0;

  constructor(private httpService: HttpService,  private titleService: Title) {
  };

  ngOnInit() {
    this.titleService.setTitle('315加盟网_小本创业致富项目大全_品牌加盟连锁创业好项目尽在315加盟网');
    // Banner轮播
    this.httpService.getBannerList().then(r => this.sliderList = r);
    // 商机排行榜
    this.httpService.getBusinessRankList().then(r => this.businessRankList = r);
    // 大家都在找
    this.httpService.getHotSearchList().then((r => this.hotSearchList = r));
    // 热门品牌
    this.getMoneyProject();
    // 品牌榜中榜
    this.httpService.getProjectRank().then(r => {
      for (let i = 0; i < r.length; i++) {
        const buff: string[] = (r[i].project.major).split(' ');
        for (let j = 0; j < buff.length; j++) {
          if (buff[j].length < 2) {
            buff.splice(j, 1);
          }
        }
        r[i].project.majors = buff;
      }
      this.projectRankList = r.slice(0, 6);
    });
  }

  changeHeatProjectEndId(id) {
    this.heatProjectEndId = id;
  }
  getMoneyPageProject(flag) {
    if (flag === 0) {
      this.moneyPageList = this.moneyPageLists.slice(6, 12);
      this.flag = 1;
    }
    if (flag === 1) {
      this.moneyPageList = this.moneyPageLists.slice(0, 6);
      this.flag = 0;
    }
  }
  getMoneyProject() {
    // 热门品牌
    this.httpService.getListByposition().then((r => {
      this.moneyPageLists = r;
      this.moneyPageList = r.slice(0, 6);
    }));
  }
}
